import menuStore from '@/stores/menu';
import { LOGIN } from '@/common/static';
import router from '../index';
import handleRouter from './handleRouter';
export const initDynamicRouter = async () => {
	const menu = menuStore();
	try {
		await menu.getList();
		if (!menu.menuList.length) return router.replace(LOGIN);
		const list = handleRouter(menu.menuList);
		list.forEach((item: any) => {
			if (item.meta.alone === true && (item.children === undefined || item.children.length == 0)) {
				router.addRoute(item);
			} else {
				router.addRoute('layout', item);
			}
		});
		router.addRoute({
			path: '/:pathMatch(.*)*',
			name: 'error',
			component: () => import('@/views/error/404.vue'),
		});
	} catch (error) {
		router.replace(LOGIN);
		return;
	}
};
